/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package multiface.web.util;

import it.sauronsoftware.base64.Base64;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import multiface.awwadco.jpa.Mimetype;
import multiface.awwadco.jpa.Opzioni;
import multiface.awwadco.jpa.PersistenceManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.codemonkey.simplejavamail.TransportStrategy;

/**
 *
 * @author federico
 */
public class Mail {

    /*private static Opzioni CFG;
     private static Log log = LogFactory.getLog(Mail.class);
     private EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
     private EntityManager em = emf.createEntityManager();*/

    /*private class SMTPAuthenticator extends javax.mail.Authenticator {

     @Override
     public PasswordAuthentication getPasswordAuthentication() {

     String username = CFG.getSmtpUser();
     String password = Base64.decode(CFG.getSmtpPassword());
     ////System.out.println("Password SMTP: "+password);

     return new PasswordAuthentication(username, password);
     }
     }*/
    //@SuppressWarnings("static-access")
    public Mail() {
        /*try {
         this.CFG = new Opzioni();
         this.CFG = em.find(Opzioni.class, 1);
         em.refresh(this.CFG);

         } catch (Exception ex) {
         log.error(ex);
         }*/
    }

    public void InvioMailStandard(String destinatario, String oggetto, String testo) throws Exception {



        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();


        Opzioni opz = em.find(Opzioni.class, 1);
        em.refresh(opz);
        em.close();

        final Email email = new Email();

        email.setFromAddress("Awwad.Co", opz.getEmailContatto());
        email.setSubject(oggetto);
        email.addRecipient(null, destinatario, Message.RecipientType.TO);
        email.setTextHTML(testo);

        if (opz.getSmtpStrategy().equalsIgnoreCase("SMTP_PLAIN")) {
            Mailer m = new Mailer(opz.getSmtpHost(), opz.getSmtpPort(), opz.getSmtpUser(), Base64.decode(opz.getSmtpPassword()), TransportStrategy.SMTP_PLAIN);
            m.setDebug(true);
            m.sendMail(email);
            return;
        }
        if (opz.getSmtpStrategy().equalsIgnoreCase("SMTP_TLS")) {
            Mailer m = new Mailer(opz.getSmtpHost(), opz.getSmtpPort(), opz.getSmtpUser(), Base64.decode(opz.getSmtpPassword()), TransportStrategy.SMTP_TLS);
            m.setDebug(true);
            m.sendMail(email);
            return;
        }
        if (opz.getSmtpStrategy().equalsIgnoreCase("SMTP_SSL")) {
            Mailer m = new Mailer(opz.getSmtpHost(), opz.getSmtpPort(), opz.getSmtpUser(), Base64.decode(opz.getSmtpPassword()), TransportStrategy.SMTP_SSL);
            m.setDebug(true);
            m.sendMail(email);
            return;
        }




        /*System.out.println("MAIL CONTATTO : " + CFG.getEmailContatto());
         System.out.println("SMTP_AUTH_USER : " + CFG.getSmtpUser());
         System.out.println("SMTP_AUTH_PSW : " + CFG.getSmtpPassword());
         System.out.println("SERVER HOST : " + CFG.getSmtpHost());

         Properties props = new Properties();
         props.put("mail.transport.protocol", "smtp");
         props.put("mail.smtp.host", Mail.CFG.getSmtpHost().trim());
         props.put("mail.smtp.auth", "true");
         props.put("mail.smtp.starttls.enable", "true");

         Authenticator auth = new SMTPAuthenticator();
         Session mailSession = Session.getDefaultInstance(props, auth);
         // uncomment for debugging infos to stdout
         mailSession.setDebug(true);
         Transport transport = mailSession.getTransport();

         MimeMessage message = new MimeMessage(mailSession);
         message.setSubject(oggetto);
         message.setContent(testo, "text/html");
         message.setFrom(new InternetAddress(Mail.CFG.getEmailContatto()));

         message.addRecipient(Message.RecipientType.TO, new InternetAddress(destinatario));


         transport.connect();
         transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
         transport.close();*/

    }
}
